var url= require('url');
var template= require('templateConfig');
var main= require('main');
var form= require('form');
var module= require('module');

//模板配置
var tplArr= [];
var Tpl= require('raw!../template/new-address.txt'); 
tplArr['new']= template.compile(Tpl.replace(/^\s*|\s*$/g, "")); 
 
var addressId = main.GetQueryString('address_id');//获取任务id
var xId = main.GetQueryString('id');//获取任务id

function Index(){
    _this= this;
    this.main= function(){
        main.event(this,'tap','i-click'); 
        this.init();
    }     

    this.getContent= function(){
        if(addressId){
            main.post(url.postSelectAddress,{address_id:addressId},function(res){
                var ret = res.data;
                var data={
                    "tType": "content",
                    "name":ret.name,
                    "mobile":ret.mobile,
                    "Daddress":ret.address,
                    "is_default":ret.is_default,
                    "address_id":addressId,
                    "address":module.selectAddress(),
                    "link":"javascript:;",
                    "click":"postForm",
                }
                $(".content").html(tplArr['new'](data));
                module.getSelectAddress($(".ui-form-item select[name='province']"),0);
                $(document).find(".form_style").attr("action",url.postEditAddress);
            })
        }else{
            var data= {
                "tType": "content",
                "address":module.selectAddress(),
                "link":"javascript:;",
                "click":"postForm",
            }
            $(".content").html(tplArr['new'](data));
            module.getSelectAddress($(".ui-form-item select[name='province']"),0);
            $(document).find(".form_style").attr("action",url.postAddAddress);
        }
    }

    this.postForm=function(ths){
        if(ths.attr('isSubmit') == 'false'){return false;}
        form.ajaxSubmit('.form_style',_this,ths); 
    }
    //callback
    this.formCallback = function(ret){
        // alert(ret.errcode);
        if (ret.code == 1) {
            window.location.href=url.Caddress+'?id='+xId;
        }
    }
    this.getFooter= function() {
    	var data= {
    		tType: "footer",
    		active: "2",
    		dataList: [{
    			link: url.Index,
    			icon: "ui-icon-home",
    			name: "首页"
    		},{
    			link: url.shopCar,
    			icon: "ui-icon-cart",
    			name: "购物车"
    		},{
    			link: url.Center,
    			icon: "ui-icon-personal",
    			name: "个人中心"
    		}]
    	}
    	$(".wrap").append(module.getModule(data, "footer"));
    }

    this.init= function(){ 
    	this.getContent();
        this.getFooter();
           
    }
    return this.main();
};

var index= new Index();